package cn.fjsj.pubsub;

import cn.fjsj.constant.GlobalConstant;
import org.redisson.Redisson;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

public class Sub {
    public static void main(String[] args) {
        Config config = new Config();
        config.useSingleServer()
                .setAddress(GlobalConstant.REDIS_ADDRESS);
        RedissonClient redissonClient = Redisson.create(config);
        RTopic topic = redissonClient.getTopic(GlobalConstant.TOPIC_NAME);
        // 订阅
        topic.addListener(Msg.class, (channel, msg) -> {
            System.out.println("channel : " + channel.toString());
            System.out.println("msg : " + msg);
        });
        System.out.println("线程不会阻塞, 可以取消订阅");
        // topic.removeListener(listenerId);
    }
}
